package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetmealDao;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private RedisTemplate redisTemplate;
    /*新增套餐*/
    @Override
    public void add(Setmeal setmeal, List<Long> checkgroupIds) {
        setmealDao.add(setmeal);
        String imageUrlInDatabase =setmeal.getImg(); // 图片在数据库的ID或路径
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, imageUrlInDatabase);
    }
    /*编辑套餐*/
    @Override
    public void edit(Setmeal setmeal, List<Long> checkGroupIds) {
        setmealDao.edit(setmeal);
    }
    /*根据id查询套餐*/
    @Override
    public Setmeal findById(Long id) {
        return setmealDao.findById(id);
    }
    /*分页查询*/
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<Setmeal> page = setmealDao.pageQuery(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }
}
